package com.nhjf.dao.teachers;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.teachers.OrganizationHasStudent;
import com.nhjf.pojo.qo.teachers.OrganizationHasStudentQo;
@Repository
public class OrganizationHasStudentDao extends BaseDao<OrganizationHasStudent,OrganizationHasStudentQo>{


	@Override
	protected Criteria buildCriteria(Criteria criteria, OrganizationHasStudentQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {
			criteria.createAlias("student", "student");
			if(qo.getOrganizationId()!=null) {
				criteria.setFetchMode("organization",FetchMode.SELECT);
				criteria.add(Restrictions.eq("organization.id",qo.getOrganizationId()));
			}
			if(qo.getStudentId()!=null) {
				criteria.add(Restrictions.eq("student.id",qo.getStudentId()));
			}
			if(StringUtils.isNotEmpty(qo.getName())) {

				criteria.add(Restrictions.like("student.nickname",qo.getName(),MatchMode.ANYWHERE));
			}
			if(StringUtils.isNotEmpty(qo.getNoteName())) {

				criteria.add(Restrictions.like("student.studentNoteName",qo.getNoteName(),MatchMode.ANYWHERE));
			}
			if(StringUtils.isNotEmpty(qo.getPhone())) {

				criteria.add(Restrictions.like("student.phonenumber",qo.getPhone(),MatchMode.ANYWHERE));
			}
			criteria.addOrder(Order.asc("student.registrationtime"));
		}
		return criteria;
	}

	@Override
	protected Class<OrganizationHasStudent> getEntityClass() {
		// TODO Auto-generated method stub
		return OrganizationHasStudent.class;
	}

}
